package com.ck.company.controller;

import com.ck.company.service.CpProgramTemplateService;
import com.ck.config.CustomProperties;
import com.ck.constant.ResponseConstant;
import com.ck.model.common.sys.JsonResponse;
import com.ck.model.common.sys.LoginUser;
import com.ck.util.SessionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 节目模板
 *
 * @author linqiang
 * @version 2.0
 * @date 2018-09-11 11:50
 */
@Controller
@RequestMapping("/programTemplate")
public class CpProgramTemplateController {
    private static Logger logger = LoggerFactory.getLogger(CpProgramTemplateController.class);

    @Autowired
    private CpProgramTemplateService cpProgramTemplateService;
    @Autowired
    private CustomProperties customProperties;

    /**
     * 查询模板尺寸(下拉列表)
     *
     * @return JsonResponse
     */
    @RequestMapping(value = "/listResolution")
    @ResponseBody
    public Object listResolution() {
        try {
            return new JsonResponse(ResponseConstant.CODE_SUCCESS, customProperties.getProgramTemplateResolution(), "查询模板尺寸成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("CpProgramTemplateController listResolution 查询模板尺寸失败: " + e.getMessage());
        }
        return new JsonResponse(ResponseConstant.CODE_FAIL, "查询模板尺寸失败");
    }

    /**
     * 删除节目模板
     *
     * @param programTemplateId 主键
     * @param request           请求
     * @return JsonResponse
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object deleteProgramTemplate(Long programTemplateId, HttpServletRequest request) {
        if (null == programTemplateId) {
            return new JsonResponse(ResponseConstant.CODE_FAIL, "参数不能为空");
        }
        try {
            LoginUser loginUser = SessionUtils.getLoginUserInfo(request);
            int row = cpProgramTemplateService.deleteByPrimaryKey(programTemplateId, loginUser);
            if (row == -1) {
                return new JsonResponse(ResponseConstant.CODE_FAIL, "预置模板不能删除");
            }
            return new JsonResponse(ResponseConstant.CODE_SUCCESS, "删除节目模板成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("CpProgramTemplateController deleteProgramTemplate 删除节目模板失败: " + e.getMessage());
        }
        return new JsonResponse(ResponseConstant.CODE_FAIL, "删除节目模板失败");
    }
}
